<template>
  <div class="group-visit">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <div class="container">
        <h1 class="page-title">{{ "组团观展" }}</h1>
        <p class="page-subtitle">
          {{ pageData.subtitle || "为团体观众提供专业的参观服务" }}
        </p>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('group-visit')"
            :class="['nav-item', { active: activeTab === 'group-visit' }]"
          >
            组团参观
          </a>
          <a
            @click="setActiveTab('visitor-preregistration')"
            :class="[
              'nav-item',
              { active: activeTab === 'visitor-preregistration' },
            ]"
          >
            观众预登记
          </a>
          <a
            @click="setActiveTab('visitor-notice')"
            :class="['nav-item', { active: activeTab === 'visitor-notice' }]"
          >
            观众须知
          </a>
          <a
            @click="setActiveTab('visa-system')"
            :class="['nav-item', { active: activeTab === 'visa-system' }]"
          >
            签证申请
          </a>
          <a
            @click="setActiveTab('visitor-roster')"
            :class="['nav-item', { active: activeTab === 'visitor-roster' }]"
          >
            展商名录
          </a>
          <a
            @click="setActiveTab('visitor-reviews')"
            :class="['nav-item', { active: activeTab === 'visitor-reviews' }]"
          >
            观众评语
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="group-section">
        <!-- <h2 class="section-title">{{ pageData.title || "组团观展" }}</h2> -->
        <div class="group-content">
          <div class="group-info">
            <h3>团体参观服务</h3>
            <p>
              为团体观众提供专业的参观服务，包括交通安排、住宿推荐、商务对接等全方位支持。
            </p>
            <div class="group-benefits">
              <div class="benefit-item">
                <el-icon><Van /></el-icon>
                <span>专车接送</span>
              </div>
              <div class="benefit-item">
                <el-icon><Location /></el-icon>
                <span>住宿安排</span>
              </div>
              <div class="benefit-item">
                <el-icon><Connection /></el-icon>
                <span>商务对接</span>
              </div>
              <div class="benefit-item">
                <el-icon><Star /></el-icon>
                <span>专属服务</span>
              </div>
            </div>
          </div>
          <div class="group-form">
            <form
              @submit.prevent="submitGroupRegistration"
              class="group-registration-form"
            >
              <div class="form-group">
                <label for="groupName">团体机构名称 *</label>
                <input
                  type="text"
                  id="groupName"
                  v-model="groupForm.groupName"
                  required
                />
              </div>
              <div class="form-group">
                <label for="groupSize">预计参观人数 *</label>
                <input
                  type="number"
                  id="groupSize"
                  v-model.number="groupForm.groupSize"
                  required
                  min="1"
                />
              </div>
              <div class="form-group">
                <label for="visitDate">参观日期 *</label>
                <input
                  type="date"
                  id="visitDate"
                  v-model="groupForm.visitDate"
                  required
                />
              </div>
              <div class="form-group">
                <label for="products">关注产品 *</label>
                <div class="checkbox-group">
                  <input
                    type="text"
                    id="products"
                    v-model="groupForm.products"
                    required
                    class="tips-b"
                  />
                  <el-checkbox-group v-model="groupForm.products" required>
                    <el-checkbox label="机器人核心零部件（含成熟和创新产品）" />
                    <el-checkbox label="大脑相关（人工智能算法及其他）" />
                    <el-checkbox label="整机研发、设计、系统集成" />
                    <el-checkbox label="机器人检测、维修和保养、测试验证等" />
                    <el-checkbox label="机器人生产设备、流水线、代工厂等" />
                    <el-checkbox value="other" style="height: auto">
                      <div
                        style="display: flex; flex-direction: column; gap: 8px"
                      >
                        <span>其他，请说明</span>
                        <input
                          type="text"
                          v-model="groupForm.otherVal"
                          v-show="isOtherChecked"
                          placeholder="请输入其他产品类型..."
                        />
                      </div>
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
              </div>
              <div class="form-group">
                <label for="visitPurposeCheckout">参观目的 *</label>
                <div class="checkbox-group">
                  <input
                    type="text"
                    id="products"
                    v-model="groupForm.visitPurposeCheckout"
                    required
                    class="tips-b"
                  />
                  <el-checkbox-group
                    v-model="groupForm.visitPurposeCheckout"
                    required
                  >
                    <el-checkbox label="了解新技术" />
                    <el-checkbox label="寻找供应商" />
                    <el-checkbox label="商务合作" />
                    <el-checkbox label="市场调研" />
                    <el-checkbox label="学习交流" />
                  </el-checkbox-group>
                </div>
              </div>
              <div class="form-group">
                <label for="specialRequirements">特殊需求 *</label>
                <textarea
                  type="text"
                  id="specialRequirements"
                  v-model="groupForm.specialRequirements"
                  placeholder="请输入特殊需求（可选）"
                />
              </div>
              <div class="form-row">
                <div class="form-group">
                  <label for="contactName">联系人姓名 *</label>
                  <input
                    type="text"
                    id="contactName"
                    v-model="groupForm.contactName"
                    required
                  />
                </div>
                <div class="form-group">
                  <label for="contactPosition">联系人职务 *</label>
                  <input
                    type="text"
                    id="contactPosition"
                    v-model="groupForm.contactPosition"
                    required
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="form-group">
                  <label for="contactPhone">联系人电话 *</label>
                  <input
                    type="tel"
                    id="contactPhone"
                    v-model="groupForm.contactPhone"
                    required
                  />
                </div>
                <div class="form-group">
                  <label for="contactEmail">联系人邮箱</label>
                  <input
                    type="email"
                    id="contactEmail"
                    v-model="groupForm.contactEmail"
                  />
                </div>
              </div>
              <button type="submit" class="submit-btn">提交组团申请</button>
            </form>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { Van, Location, Connection, Star } from "@element-plus/icons-vue";
import { subpageApi, groupVisitApi } from "@/api/user";

export default {
  name: "GroupVisit",
  components: {
    Van,
    Location,
    Connection,
    Star,
  },
  computed: {
    isOtherChecked() {
      return this.groupForm.products.includes("other");
    },
  },
  setup() {
    const activeTab = ref("group-visit");
    const pageData = ref({});
    const bannerImage = ref("");

    // 组团表单数据
    const groupForm = ref({
      groupName: "",
      groupSize: null,
      visitDate: "",
      products: [],
      contactName: "",
      contactPosition: "",
      contactPhone: "",
      contactEmail: "",
      specialRequirements: "", // 特殊需求
      otherVal: "",
      visitPurposeCheckout: [], // 参观目的
    });

    const loadPageData = async () => {
      try {
        const response = await subpageApi.getSubpageList(
          "visitors",
          "group-visit"
        );
        if (
          response &&
          response.code === "200" &&
          response.data &&
          response.data.length > 0
        ) {
          // 取第一条作为页面基本信息
          const firstItem = response.data[0];
          pageData.value = {
            title: firstItem.title || "组团观展",
            subtitle: firstItem.description || "为团体观众提供专业的参观服务",
            content: firstItem.content || "",
          };
        } else {
          // 使用默认数据
          pageData.value = {
            title: "组团观展",
            subtitle: "为团体观众提供专业的参观服务",
            content: "",
          };
        }
      } catch (error) {
        console.error("加载组团观展数据失败:", error);
        pageData.value = {
          title: "组团观展",
          subtitle: "为团体观众提供专业的参观服务",
          content: "",
        };
      }
    };

    const loadBanner = async () => {
      try {
        // 与观众中心入口页保持一致：统一取 carousel_visitors 顶部图
        const res = await subpageApi.getContentByType("carousel_visitors");
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data) &&
          res.data.length > 0
        ) {
          const firstActive =
            res.data.find((item) => item.status === "active") || res.data[0];
          bannerImage.value = firstActive.imageUrl || firstActive.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    // 提交组团申请
    const submitGroupRegistration = async () => {
      try {
        if (groupForm.value.products.includes("other")) {
          groupForm.value.products.forEach((item, index) => {
            if (item === "other")
              groupForm.value.products[index] = groupForm.value.otherVal;
          });
        }
        // console.log(groupForm.value);
        // return;
        const payload = {
          groupName: groupForm.value.groupName,
          groupSize: groupForm.value.groupSize,
          visitPurpose: groupForm.value.products.join(","),
          organizerName: groupForm.value.contactName,
          organizerPhone: groupForm.value.contactPhone,
          organizerEmail: groupForm.value.contactEmail,
          groupType: groupForm.value.contactPosition,
          specialRequirements: groupForm.value.specialRequirements,
          visitPurposeCheckout: groupForm.value.visitPurposeCheckout.join(","),
          contactAddress: "",
          organizationCertificate: "",
          visitDate: groupForm.value.visitDate,
        };
        const res = await groupVisitApi.apply(payload);
        if (res && (res.code === "200" || res.code === 200)) {
          ElMessage.success("组团申请提交成功！我们会尽快与您联系。");
          Object.keys(groupForm.value).forEach(
            (k) => (groupForm.value[k] = k === "groupSize" ? null : "")
          );
        } else {
          ElMessage.error(res?.msg || "提交失败，请重试");
        }
      } catch (e) {
        ElMessage.error(e.message || "提交失败，请重试");
      }
    };

    onMounted(() => {
      loadPageData();
      loadBanner();
    });

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      if (tabKey !== "group-visit") {
        window.location.href = `/${tabKey}`;
      }
    };

    return {
      activeTab,
      pageData,
      bannerImage,
      groupForm,
      submitGroupRegistration,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.group-visit {
  min-height: 100vh;
  background: #f8f9fa;
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  margin-top: 0;
  text-align: center;
  color: white;
}

.page-header.has-bg {
  height: 400px;
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.group-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
}

.group-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3rem;
  max-width: 1000px;
  margin: 0 auto;
}

.group-info h3 {
  font-size: 1.8rem;
  color: var(--e-global-color-title);
  margin-bottom: 1rem;
}

.group-info p {
  color: #666;
  line-height: 1.8;
  font-size: 1.1rem;
  margin-bottom: 2rem;
}

.group-benefits {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
}

.benefit-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem;
  background: linear-gradient(135deg, #f8f9ff 0%, #e8f0ff 100%);
  border-radius: 10px;
  border: 1px solid #e0e7ff;
}

.benefit-item .el-icon {
  color: var(--e-global-color-title);
  font-size: 1.2rem;
}

.benefit-item span {
  color: var(--e-global-color-title);
  font-weight: 600;
  font-size: 0.9rem;
}

.group-form {
  background: linear-gradient(135deg, #f8f9ff 0%, #e8f0ff 100%);
  border-radius: 15px;
  padding: 2rem;
  border: 1px solid #e0e7ff;
}

.group-registration-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 600;
  color: var(--e-global-color-title);
}

.form-group input,
.form-group textarea,
.form-group .checkbox-group {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #e0e7ff;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}
.checkbox-group {
  position: relative;
  margin-top: 10px;
  background-color: #fff;
  box-sizing: border-box;
}
.tips-b {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  z-index: -1;
}
.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--e-global-color-selected);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.submit-btn {
  background: linear-gradient(
    135deg,
    var(--e-global-color-a3699a4) 0%,
    var(--e-global-color-5c1ca3d) 100%
  );
  color: white;
  border: none;
  padding: 1rem 2rem;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.3s ease;
  margin-top: 1rem;
}

.submit-btn:hover {
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .group-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .group-content {
    grid-template-columns: 1fr;
    gap: 2rem;
  }

  .group-benefits {
    grid-template-columns: 1fr;
  }

  .form-row {
    grid-template-columns: 1fr;
  }

  .page-title {
    font-size: 2rem;
  }

  .page-subtitle {
    font-size: 1rem;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .group-section {
    padding: 1rem;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
  :deep(.el-checkbox__label) {
    font-size: 12px !important;
    word-break: break-word !important;
  }
  .group-form {
    padding: 1rem;
  }
}
:deep(.el-checkbox) {
  margin-right: 0 !important;
}
</style>
